home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 111_01 / bc.c < prev    next >
Text File  |  1985-08-19  |  3KB  |  95 lines

  1. /*
  2. HEADER:        ;
  3. TITLE:        Binary Calculator;
  4. VERSION:    1.0;
  5.  
  6. DESCRIPTION:    "Evaluates arithmetic and logic expressions entered
  7.         at the console, and prints the result in decimal,
  8.         hexadecimal, octal, binary, and ASCII notations.
  9.  
  10.         Compilation requires files BC.C, BCANLYZ.C, and BCDEF.H.
  11.         BC.DOC contains instructions for compiling and using.";
  12.  
  13. KEYWORDS:    Arithmetic, logical, expression, evaluator, calculator;
  14. SYSTEM:        CP/M-80;
  15. FILENAME:    BC.C;
  16. WARNINGS:    "BC is limited to integers, 0 to 65535.";
  17. SEE-ALSO:    BCANLYZ.C, BCDEF.H, BC.DOC, BCREVIEW.DOC;
  18. AUTHORS:    Jan Larsson;
  19. COMPILERS:    BDS C;
  20. */
  21. /************************************************************************/
  22. main(){
  23. char string[100];
  24. unsigned res ;
  25. char line[255] ;
  26. puts(" + + +  bc  binary calculator  2.0  + + + \n");
  27. puts("         (c) 1981  Occam Mjukisar \n");
  28. puts("\n Want to have instructions (y/n) ?? ");
  29. if(tolower(getchar()) == 'y')tellhim();
  30. putchar('\n');putchar('\n');
  31. for(;;){
  32. printf("Enter expression : ");
  33. gets( line );
  34. lcase(line);
  35. res = anlyz( line );
  36. printf("Decimal: %6u           Hex: %04x\n",res,res);
  37. printf("Octal  : %06o           ",res);
  38. if((res & 0x007f) >= ' ')printf("Ascii:  %c\n",res & 0x007f);
  39. else printf("\n");
  40. printf("Binary : ");
  41. binout( res, 0);
  42. printf("\n\n");
  43. }
  44. }
  45.  
  46. lcase( s )
  47. char *s ;
  48. {
  49.     while(*s != '\0'){
  50.         if(*s == '\''){*s = *s ; s++ ; *s = *s ;s++;}
  51.         *s = tolower(*s);
  52.         s++ ;
  53.         }
  54. }
  55.  
  56.  
  57. tellhim(){
  58. puts("\n");
  59. puts("\n");
  60. puts(" This program acts as a general binary\n");
  61. puts(" calculator with 16-bit precision. In\n");
  62. puts(" the expression you may have any number\n");
  63. puts(" of nested parentheses and numbers may\n");
  64. puts(" be expressed in several ways.\n");
  65. puts("\n");
  66. puts(" Allowed number bases:\n");
  67. puts("           b = binary\n");
  68. puts("           o = octal\n");
  69. puts("           q = octal\n");
  70. puts("           d = decimal\n");
  71. puts("           h = hexadecimal\n");
  72. puts("         'a' = ascii value\n");
  73. puts("     default = decimal\n\n");
  74. puts("Legal operators:\n");
  75. puts("           + = plus\n");
  76. puts("           - = minus\n");
  77. puts("           * = multiplication\n");
  78. puts("           / = division\n");
  79. puts("  'mod' or % = modulus\n");
  80. puts("  'and' or & = bitwise and\n");
  81. puts("  'xor' or ~ = bitwise xor\n");
  82. puts("   'or' or | = bitwise or\n");
  83. puts("  'shl' or < = shift left, zero fill\n");
  84. puts("  'shr' or > = shift right, zero fill\n");
  85. puts("\n");
  86. puts("Unary operators:\n");
  87. puts("           - = negation\n");
  88. puts("  'not' or ~ = one's complement\n");
  89. puts("  (note that two unary operators before\n");
  90. puts("   one operand (like 'not -7') is illegal\n");
  91. puts("   but will not draw any error message)\n");
  92. puts("\n");
  93. }
  94.  
  95.